package com.app.controller.backstage;

import com.app.conf.RedisService;
import com.app.model.AdminUserModel;
import com.app.model.EnterpriseModel;
import com.app.model.LiveRecordModel;
import com.app.service.*;
import com.app.util.DateUtil;
//import com.app.util.Result;
//import com.app.util.TokenUtils;
//import com.app.vo.IndexCountVo;
//import org.json.JSONObject;
//import com.app.util.Result;
import com.app.util.Result;
import com.app.util.SpringUtils;
import com.app.util.TokenUtils;
import com.app.vo.IndexCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Controller
public class StatisticsController {

    @Autowired
    private RedisService redisService;
    @Autowired
    AdminUserService adminUserService;

    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    JobPublishService jobPublishService;

    @Autowired
    WorkApplyService workApplyService;

    @Autowired
    BrowseNumberService browseNumberService;

    @Autowired
    JobFairCompanyService jobFairCompanyService;

    @Autowired
    CustomerService customerService;

    @Autowired
    NewsService newsService;

    @Autowired
    ComResumeNumberService comResumeNumberService;
    @Autowired
    ComRecruitersService comRecruitersService;

    @GetMapping("/getIndexCount")
    public Result<Object> getCusPlaceList(
            HttpServletRequest request) {
        int enterpriseCount = comRecruitersService.getCountByGroup(null,null,2);//企业数
        EnterpriseModel model = new EnterpriseModel();
        model.setState(2);
        int enterpriseWaitCount = enterpriseService.getModelCount(model);//待审核企业数量
        int workAppCount = workApplyService.countAll(null,null,2);//投递数
        int jobFairCount = jobFairCompanyService.countAll();
        int newsCount = newsService.getCountAll();
        int comRecruitersCount = comResumeNumberService.getCountAll();
        int publishWaitCount = jobPublishService.countWait();//未审核岗位数量
        IndexCountVo vo3 = (IndexCountVo) redisService.getStringKey("dunhuangIndexCount");
        IndexCountVo vo = new IndexCountVo();
        vo.setEnterpriseCount(vo3.getEnterpriseCount());
        vo.setJobCount(vo3.getJobCount());
        vo.setWorkAppCount(131+(workAppCount*3));
        vo.setBrowseCount(vo3.getBrowseCount());
        vo.setJobFairCount(jobFairCount);
        vo.setCustomerCount(vo3.getCustomerCount());
        vo.setNewsCount(newsCount);
        vo.setPublishWaitCount(publishWaitCount);
        vo.setComRecruitersCount(comRecruitersCount);
        vo.setEnterpriseWaitCount(enterpriseWaitCount);
        return Result.success(vo);
    }

    @GetMapping("/getWorkApplyByTime")
    public Result<Object> getWorkApplyByTime(
            HttpServletRequest request) {

        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = workApplyService.getModelCountByTime(time);
            xData.add(time);
            yData.add(count);

        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);

        return Result.success(vo);
    }

    @GetMapping("/getJobPublishCensus")
    public Result<Object> getJobPublishCensus(
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = jobPublishService.getModelCountByTime(time);
            xData.add(time);
            yData.add(count);

        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);
        return Result.success(vo);
    }

//*
//
//7天新增访问数
// **
    @GetMapping("/getSevenSaysBrowse")
    public Result<Object> getSevenSaysBrowse(
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = browseNumberService.getModelCountByTime(time);
            xData.add(time);
            yData.add(count);

        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);
        return Result.success(vo);
    }


    //*
    //
    //7天新增用户数
    // **
    @GetMapping("/getSevenSaysCustomer")
    public Result<Object> getSevenSaysCustomer(
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = customerService.getModelCountByTime(time,null,null,2);
            xData.add(time);
            yData.add(count);
        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);
        return Result.success(vo);
    }



}
